package com.liuqing.www.xml解析;

/**
 * jdom算法
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.util.Hashtable;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;

import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;



public class GWXMLUtil {

	public static GWXMLUtil getInstance() {

		return new GWXMLUtil();
	}

	public static void main(String[] args) {
		GWXMLUtil.getInstance().vOperateXMLTest();
        		

	}
	
	public void vOperateXMLTest(){
		try {
			String tmpxml = this.vOpenXML("d:\\tmp\\rule-volleyball.xml","utf-8");
			//String tmpxml = this.vOpenXML("c:\\xml\\test1.xml","utf-8");
			// String tmpxml =
			// ox.openXML("E:\\work\\project\\gt\\stat\\src\\managerserver\\tmp\\exampleA.xml","");

			Hashtable xmlhsh = this.readpXML(tmpxml);
			GWStrUtil.getInstance().printStr(EvoHashToStr(readpXML(tmpxml)));
			GWStrUtil.getInstance().printStr(this.writerootXML(xmlhsh));
   			// Evocodelog.logDebug(ox.writerootXML(xmlhsh), null);
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
	}

	public void vSaveXML(String xmlstr,String filepath, String filename, String decode) {
		try {

			xmlstr = "<?xml version=\"1.0\" encoding=\"" + decode + "\"?>"
					+ xmlstr;

			GWFileUtil.getInstance().eWriteFile(filepath,filename,xmlstr.getBytes(decode));
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
	}

	public String vOpenXML(String filepath, String decode) {

		String res="";

		try {
			byte [] bt=GWFileUtil.getInstance().eReadFile(filepath);
			res=new String(bt,decode);
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return res;
	}
	
	public Hashtable vOpenXMLX(String filepath, String decode) {

		Hashtable res=new Hashtable();

		try {
			byte [] bt=GWFileUtil.getInstance().eReadFile(filepath);
			String tmpstr=new String(bt,decode);
			

			res = this.readpXML(tmpstr);
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return res;
	}

	public Hashtable readXML(String tmpxml) {
		Hashtable ht = new Hashtable();
		Hashtable rootht = new Hashtable();

		try {

			SAXBuilder builder = new SAXBuilder();
			// String inputstr=new String(tmpxml.getBytes("utf-8"),"utf-8");
			InputStream in = null;
			Document doc = null;

			String tmltrxml = tmpxml.replaceAll("&", "");
			// String tmltrxml=tmpxml.trim();
			in = new ByteArrayInputStream(tmltrxml.getBytes("utf-8"));
			doc = builder.build(in);

			Element root = doc.getRootElement();

			List lt = root.getChildren();

			for (int i = 0; i < lt.size(); i++) {

				Element e = (Element) lt.get(i);
				List lt1 = e.getChildren();
				List lt2 = e.getAttributes();
				if (lt1.size() > 0) {

					ht.put(String.valueOf(i) + ":" + e.getName(), this
							.getxmlChildren(lt1));
				} else {
					ht.put(String.valueOf(i) + ":" + e.getName(), e.getText()
							.trim());
				}

				if (lt2.size() > 0) {
					Hashtable hsatt = new Hashtable();
					for (int j = 0; j < lt2.size(); j++) {
						Attribute a1 = (Attribute) lt2.get(j);
						hsatt.put(a1.getName(), a1.getValue());
						GWStrUtil.getInstance().printStr(lt2.get(j)+"----------Attribute");
					}
					ht.put(String.valueOf(i) + ":att:" + e.getName(), hsatt);

				}
			}

			// Evocodelog.logDebug(ht, this);
			rootht.put("0:" + root.getName(), ht);
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return rootht;
	}

	public Hashtable getxmlChildren(List lt) {
		Hashtable hs1 = new Hashtable();
		try {
			for (int i = 0; i < lt.size(); i++) {

				Element e = (Element) lt.get(i);
				//GWStrUtil.getInstance().printStr(lt.get(i)+"------abc");
				List lt1 = e.getChildren();
				//GWStrUtil.getInstance().printStr(e.getName()+"--------children");
				List lt2 = e.getAttributes();

				if (lt1.size() > 0) {
					hs1.put(String.valueOf(i) + ":" + e.getName(), this
							.getxmlChildren(lt1));

				} else {
					hs1.put(String.valueOf(i) + ":" + e.getName(), e.getText()
							.trim());
				}

				if (lt2.size() > 0) {
					Hashtable hsatt = new Hashtable();
					for (int j = 0; j < lt2.size(); j++) {
						Attribute a1 = (Attribute) lt2.get(j);
						hsatt.put(a1.getName(), a1.getValue());
					}
					hs1.put(String.valueOf(i) + ":att:" + e.getName(), hsatt);
				}

			}
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return hs1;
	}

	public Hashtable readpXML(String tmpxml) {
		Hashtable ht = new Hashtable();
		Hashtable rootht = new Hashtable();
		try {

			SAXBuilder builder = new SAXBuilder();
			// String inputstr=new String(tmpxml.getBytes("utf-8"),"utf-8");
			InputStream in = null;
			Document doc = null;

			String tmltrxml = tmpxml.replaceAll("&", "");
			// String tmltrxml=tmpxml.trim();
			in = new ByteArrayInputStream(tmltrxml.getBytes("utf-8"));
			doc = builder.build(in);

			Element root = doc.getRootElement();

			List lt = root.getChildren();

			for (int i = 0; i < lt.size(); i++) {

				Element e = (Element) lt.get(i);
				List lt1 = e.getChildren();
				// / List lt2 = e.getAttributes();
				if (lt1.size() > 0) {

					ht.put(e.getName(), this.getxmlpChildren(lt1));
				} else {
					ht.put(e.getName(), e.getText().trim());
				}

			}

			// Evocodelog.logDebug(ht, this);
			rootht.put(root.getName(), ht);
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return rootht;
	}

	public Hashtable getxmlpChildren(List lt) {
		Hashtable hs1 = new Hashtable();
		try {
			for (int i = 0; i < lt.size(); i++) {

				Element e = (Element) lt.get(i);
				List lt1 = e.getChildren();

				if (lt1.size() > 0) {
					hs1.put(e.getName(), this.getxmlpChildren(lt1));

				} else {
					hs1.put(e.getName(), e.getText().trim());
				}

			}
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return hs1;
	}
//////
	public String EvoHashToStr(Hashtable ht) {
		String res = "";
		Object[] oj = ht.keySet().toArray();
		for (int i = 0; i < oj.length; i++) {
			if (ht.get(oj[i]) instanceof Hashtable)
			{
				res = res + "<" + (String) oj[i] + ">"
						+ this.EvoHashToStr((Hashtable) ht.get(oj[i])) + "</"
						+ (String) oj[i] + ">";
			//	GWStrUtil.getInstance().printStr("you yunxnmg");
			} else 
			{
				res = res + "<" + (String) oj[i] + ">" + (String) ht.get(oj[i])
						+ "</" + (String) oj[i] + ">";
//GWStrUtil.getInstance().printStr((String) oj[i]+"   ��ǩ");
//GWStrUtil.getInstance().printStr( (String) ht.get(oj[i])+"   ����");

			}
		}
		
		return res;
	}

	public String writerootXML(Hashtable xmlhsh) {
		String resstr = "";
		try {
			Object[] keyo = xmlhsh.keySet().toArray();
			Element root = new Element(this.decodestr((String) keyo[0]));
			Document doc = new Document(root);
			root = writeXML((Hashtable) xmlhsh.get((String) keyo[0]), root);
			Format format = Format.getCompactFormat();
			format.setEncoding("utf-8");
			format.setIndent("    ");
			XMLOutputter XMLOut = new XMLOutputter(format);
			// XMLOut.output(doc, new FileOutputStream("d:\\A.xml"));
			ByteArrayOutputStream bao = new ByteArrayOutputStream();
			XMLOut.output(doc, bao);
			resstr = new String(bao.toByteArray());
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}

		return resstr;
	}

	public Element writeXML(Hashtable xmlhsh, Element u1) {

		String xmlstring = "";
		try {

			Object[] tmpstr = xmlhsh.keySet().toArray();

			String attflag = "0";

			for (int ii = 0; ii < tmpstr.length; ii++) {
				attflag = "0";
				String k1 = (String) tmpstr[ii];
				String k = this.decodestr(k1);
				if (k.indexOf("att:") > -1) {
					k = this.decodestr(k);
					attflag = "1";
					continue;
				}

				Element u = new Element(k);

				if (xmlhsh.get(k1) instanceof Hashtable) {

					u = this.writeXML((Hashtable) xmlhsh.get(k1), u);
				} else {
					if (attflag.equals("1")) {
						// u.setAttribute(, );
						// Attribute ba=new Attribute(k,(String)xmlhsh.get(k1));

						// u1.setAttribute(ba);
						continue;
					} else {

						String tmpstr1 = new String(((String) xmlhsh.get(k1)));
						u.addContent(tmpstr1);
					}

				}

				u1.addContent(u);
			}
			for (int ii = 0; ii < tmpstr.length; ii++) {
				attflag = "0";
				String k1 = (String) tmpstr[ii];
				String k = this.decodestr(k1);
				if (k.indexOf("att:") > -1) {
					k = this.decodestr(k);
					Element u = (Element) u1.getChild(k);
					Hashtable tmpstt = (Hashtable) xmlhsh.get(k1);
					Object[] attob = tmpstt.keySet().toArray();
					for (int m = 0; m < attob.length; m++) {
						u.setAttribute((String) attob[m], (String) tmpstt
								.get((String) attob[m]));
					}
				}
			}
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return u1;
	}

	public String decodestr(String decstr) {
		String tmpstr = "";
		try {
			tmpstr = decstr.substring(decstr.indexOf(":") + 1, decstr.length());
		} catch (Exception e) {
			GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
		}
		return tmpstr;
	}

	public Hashtable hashdecode(Hashtable dechs) {

		Object[] tmpstr = dechs.keySet().toArray();

		for (int i = 0; i < tmpstr.length; i++) {

			try {
				String k = (String) tmpstr[i];

				Object tmpobj = dechs.get(k);

				if (tmpobj instanceof String) {

					dechs.put(this.decodestr(k), (String) tmpobj);

					dechs.remove(k);

				} else {

					dechs.put(this.decodestr(k), this
							.hashdecode((Hashtable) tmpobj));
					dechs.remove(k);

				}

			} catch (Exception e) {
				GWLogUtil.getInstance().ELogDebug(this.getClass().getName(),e);
			}

		}

		return dechs;

	}

	// public int nindex=0;

	public String[][] evoHashtoarr(Hashtable rhash) {

		String[][] resarr = null;
		Object[] rh = rhash.keySet().toArray();
		resarr = new String[rh.length][4];
		for (int i = 0; i < resarr.length; i++) {
			String[] tmpname = (String[]) rh[i];
			resarr[i][0] = tmpname[0];
			resarr[i][1] = tmpname[1];
			resarr[i][2] = tmpname[2];
			resarr[i][3] = (String) rhash.get(tmpname);
		}

		return resarr;

	}

	public Hashtable evoHashtoAL(Hashtable rhash, int praindex, int nindex) {
		Hashtable ehsah = new Hashtable();
		Object[] rh = rhash.keySet().toArray();
		nindex = nindex + 1;
		int uindex = nindex;
		for (int i = 0; i < rh.length; i++) {
			String[] tmpname = new String[3];
			if ((rhash.get(rh[i])) instanceof Hashtable) {
				tmpname[0] = (String) rh[i];
				tmpname[1] = String.valueOf(praindex);
				tmpname[2] = String.valueOf(uindex);
				ehsah.put(tmpname, "node");
				ehsah.putAll(this.evoHashtoAL((Hashtable) rhash.get(rh[i]),
						uindex, nindex));

			} else {
				tmpname[0] = (String) rh[i];
				tmpname[1] = String.valueOf(praindex);
				tmpname[2] = "info";
				ehsah.put(tmpname, (String) rhash.get(rh[i]));
			}
		}
		return ehsah;
	}

}
